package org.oos.lists;

import java.util.AbstractList;
import java.util.List;

import org.oos.OOS4Java;
import org.oos.atom.entries.SiteEntry;
import org.oos.model.Site;
import org.oos.util.Cache;

public class SiteList extends AbstractList<Site> {

	private transient OOS4Java oos4Java;
	private List<SiteEntry> list;
	private Cache<SiteEntry, Site> cache;

	public SiteList(OOS4Java oos4Java, List<SiteEntry> list) {
		this.oos4Java = oos4Java;
		this.list = list;
	}

	public Cache<SiteEntry, Site> getCache() {
		if(cache == null) {
			cache = new Cache<SiteEntry, Site>();
		}
		return cache;
	}
	
	@Override
	public Site get(int location) {
		SiteEntry entry = list.get(location);
		Site result;
		if (getCache().containsKey(entry)) {
			result = getCache().get(entry);
		} else {
			result = new Site(oos4Java, list.get(location));
			getCache().put(entry, result);
		}
		return result;
	}

	@Override
	public int size() {
		return list.size();
	}
	
	public void setOos4Java(OOS4Java oos4Java) {
		this.oos4Java = oos4Java;
	}
}
